//
//  ICDefaultConfigManager.m
//  ICome
//
//  Created by zhangrongwu on 2016/11/16.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICDefaultConfigManager.h"
#import "ICDatabaseManager.h"
#import "ICHeaderFile.h"

@implementation ICDefaultConfigManager
    
+(instancetype)sharedInstance {
    static dispatch_once_t once;
    static id def;
    dispatch_once(&once, ^{
        def = [[ICDefaultConfigManager alloc] init];
    });
    return def;
}

-(instancetype)init {
    if (self = [super init]) {
        [self addObserver];
    }
    return self;
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}


- (void)applicationEnterForeground {
    [ICJSBridgeTools getJsTicket:nil];
    
    if (![ICTools stringEmpty:[ICTools getToken]]) {
        if (![[ICAsyncSocketManager sharedInstance] isConnected]) {
            [[ICAsyncSocketManager sharedInstance] reconnectionSuccess:nil Failure:nil];
        }
    }
}

- (void)applicationEnterBackground {
    [self endTimer];
    [[ICAsyncSocketManager sharedInstance] disconnectSocket];
}

//- (void)startTimer {
//    [_ticketTimer invalidate];
//}

- (void)endTimer {
    [_ticketTimer invalidate];
    _ticketTimer = nil;
}

- (void)initTCP {
    [[ICAsyncSocketManager sharedInstance] initSocket];
    [self getJsTicket];
    self.getTicketCount = 0;
}

- (void)getJsTicket
{
    WEAKSELF
    [ICJSBridgeTools getJsTicket:^(BOOL isSuccess, id object) {
        if (isSuccess) {
          
            int expires = [[[object objectForKey:@"data"] objectForKey:@"expires_in"] intValue];
            if (expires > 100) {
                
              weakSelf.ticketTimer = [NSTimer timerWithTimeInterval:expires target:weakSelf selector:@selector(getTicket) userInfo:nil repeats:YES];
                [[NSRunLoop currentRunLoop] addTimer:weakSelf.ticketTimer forMode:NSRunLoopCommonModes];
            }
        } else {
         // 失败
            if (weakSelf.getTicketCount < 3) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    weakSelf.getTicketCount++;
                    [weakSelf getJsTicket];
                });
            }
        }
    }];
}

- (void)getTicket
{
    [self getJsTicket];
    [self.ticketTimer invalidate];
}

- (void)createDefaultGroups {
    
    if (![ICMessageDatabase isGroupExisted:[NSString stringWithFormat:@"tdo_%@", [ICUser currentUser].eId]]) {
        [self createNoticeGroup];// 通知组
        [self createPendingGroup];//待办
        //    [self createChnGroup];//机会
//        [self createRobotGroup];//平台助手
//        [self createServiceGroup];//客服
        //    [self createDirectGroup]; // 送达号统一入口
    }
}

// 创建待办组
-(void)createPendingGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"tdo_%@",[ICUser currentUser].eId];
    group.gType = @"3";
    group.gName = @"待办消息";
    group.lastMsgString = @"您没有待办消息";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:0];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999999];
}
// 创建通知组
- (void)createNoticeGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"ntf_%@",[ICUser currentUser].eId];
    group.gType = @"8";
    group.gName = @"系统通知";
    group.lastMsgString = @"您没有通知消息";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:0];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999998];
}
// 创建机会组
-(void)createChnGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"chn_%@",[ICUser currentUser].eId];
    group.gType = @"10";
    group.gName = @"机会";
    group.lastMsgString = @"查看机会";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:0];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999997];
}
    
// 平台客服
-(void)createServiceGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"svr_%@",[ICUser currentUser].eId];
    group.gType = @"13";
    group.gName = @"平台客服";
    group.lastMsgString = @"我是客服";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:0];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999996];
}

// 平台助手
-(void)createRobotGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"rbt_%@",[ICUser currentUser].eId];
    group.gType = @"12";
    group.gName = @"平台助手";
    group.lastMsgString = @"小助手";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:1];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999995];
}

// 创建待办组
-(void)createDirectGroup {
    ICGroup *group = [[ICGroup alloc] init];
    group.gId = [NSString stringWithFormat:@"dirm_%@",[ICUser currentUser].eId];
    group.gType = @"14";
    group.gName = @"企业资讯";
    group.lastMsgString = @"企业资讯消息";
    group.valid = 1;
    group.lastMsgTime = [ICMessageHelper currentMessageTime];
    [ICMessageDatabase saveGroup:group isTop:YES localType:1];
    [ICMessageDatabase makeGroupTop:group isTop:YES lastMsgTime:9999999999994];
}

@end















